package com.cardbaobao.cardbabyclient.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.callback.RecycleItemClickedCallback;
import com.cardbaobao.cardbabyclient.model.RedEnvelopesList;
import com.cardbaobao.cardbabyclient.utils.DensityUtil;
import com.cardbaobao.cardbabyclient.utils.ImageLoaderUtil;
import com.cardbaobao.cardbabyclient.utils.URLUtil;

import java.util.List;

/**
 * Created by Jiangzx on 11:37.
 */

public class DailyRedEnvelopesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<RedEnvelopesList.RedEnvelopes> mData;

    private static final int TYPE_NO_DATA=1;
    private static final int TYPE_CONTENT=2;

    private int leftRightMargin;

    private RecycleItemClickedCallback clickedCallback;
    public void setRecycleItemClickedCallback(RecycleItemClickedCallback clickedCallback){
        this.clickedCallback=clickedCallback;
    }

    public DailyRedEnvelopesAdapter(List<RedEnvelopesList.RedEnvelopes> data) {
        leftRightMargin=DensityUtil.dp2px(13);
        this.mData=data;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater=LayoutInflater.from(parent.getContext());
        if(viewType==TYPE_NO_DATA){
            View itemView=inflater.inflate(R.layout.item_no_data,parent,false);
            return new NoDataHolder(itemView);
        }
        if(viewType==TYPE_CONTENT){
            View itemView=inflater.inflate(R.layout.item_daily_red_envelopes,parent,false);
            return new ContentHolder(itemView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContentHolder) {
            ((ViewGroup.MarginLayoutParams)holder.itemView.getLayoutParams()).
                    setMargins(leftRightMargin,0,leftRightMargin,0);
            ContentHolder contentHolder = (ContentHolder) holder;
            if (position == 0) {
                RecyclerView.MarginLayoutParams params =
                        (RecyclerView.MarginLayoutParams) holder.itemView.getLayoutParams();
                params.topMargin = DensityUtil.dp2px(15);
            }
            final RedEnvelopesList.RedEnvelopes redEnvelope = mData.get(position);
            contentHolder.tvName.setText(redEnvelope.getName());
            contentHolder.tvContent.setText(redEnvelope.getMarketing_one());
            contentHolder.tvJoinCount.setText(String.valueOf(redEnvelope.getApply_count()));
            ImageLoaderUtil.displayImageWithPlaceHolder(URLUtil.CBB_IMG_URL + redEnvelope.getPicture_path(),
                    contentHolder.pic,
                    R.mipmap.img_default_square);
            contentHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (clickedCallback != null) {
                        clickedCallback.onRecycleItemClicked(holder.getAdapterPosition());
                    }
                }
            });
        }
    }

    @Override
    public int getItemViewType(int position) {
        return (mData==null||mData.size()==0)?TYPE_NO_DATA:TYPE_CONTENT;
    }

    @Override
    public int getItemCount() {
        return (mData==null||mData.size()==0)?1:mData.size();
    }

    private class ContentHolder extends RecyclerView.ViewHolder{
        private TextView tvName;
        private TextView tvContent;
        private TextView tvJoinCount;
        private ImageView pic;
        private ContentHolder(View itemView){
            super(itemView);
            tvName= (TextView) itemView.findViewById(R.id.tv_name);
            tvContent= (TextView) itemView.findViewById(R.id.tv_content);
            tvJoinCount= (TextView) itemView.findViewById(R.id.tv_join_count);
            pic= (ImageView) itemView.findViewById(R.id.image);
        }
    }

    private class NoDataHolder extends RecyclerView.ViewHolder{
        private NoDataHolder(View itemView){
            super(itemView);
            itemView.setVisibility(View.VISIBLE);
        }
    }
}
